package com.company.project.web;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.KrWokrdayDivsSums;
import com.company.project.model.KrWokrdaySums;
import com.company.project.service.KrWokrdayDivsSumsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
* Created by CodeGenerator on 2018/12/14.
*/
@RestController
@RequestMapping("/kr/wokrday/divs/sums")
public class KrWokrdayDivsSumsController {
    @Resource
    private KrWokrdayDivsSumsService krWokrdayDivsSumsService;

    @PostMapping("/add")
    public Result add(KrWokrdayDivsSums krWokrdayDivsSums) {
        krWokrdayDivsSumsService.save(krWokrdayDivsSums);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        krWokrdayDivsSumsService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(KrWokrdayDivsSums krWokrdayDivsSums) {
        krWokrdayDivsSumsService.update(krWokrdayDivsSums);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        KrWokrdayDivsSums krWokrdayDivsSums = krWokrdayDivsSumsService.findById(id);
        return ResultGenerator.genSuccessResult(krWokrdayDivsSums);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<KrWokrdayDivsSums> list = krWokrdayDivsSumsService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    public void test1() throws FileNotFoundException {
        OutputStream out = new FileOutputStream("C:\\Users\\liuyang\\79.xlsx");
        try {
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
            //写第一个sheet, sheet1  数据全是List<String> 无模型映射关系
            Sheet sheet1 = new Sheet(1, 2, KrWokrdayDivsSums.class);
            writer.write(krWokrdayDivsSumsService.findAll(), sheet1);
            writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
            writer.finish();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
